//
//  NoteViewController.swift
//  NoteLineDemo
//
//  Created by YL-Hong on 2023/1/18.
//

import UIKit
let TestNoteContent = """
醉梦里挑亮油灯观看宝剑，恍惚间又回到了当年，各个军营里接连不断地响起号角声。把酒食分给部下享用，让乐器奏起雄壮的军乐鼓舞士气。这是秋天在战场上阅兵。
战马像的卢马一样跑得飞快，弓箭像惊雷一样震耳离弦。我一心想替君主完成收复国家失地的大业，取得世代相传的美名。一梦醒来，可惜已是白发人
醉梦里挑亮油灯观看宝剑，恍惚间又回到了当年，各个军营里接连不断地响起号角声。把酒食分给部下享用，让乐器奏起雄壮的军乐鼓舞士气。这是秋天在战场上阅兵。
战马像的卢马一样跑得飞快，弓箭像惊雷一样震耳离弦。我一心想替君主完成收复国家失地的大业，取得世代相传的美名。一梦醒来，可惜已是白发人！
"""

class NoteViewController: UIViewController {

        override func viewDidLoad() {
            super.viewDidLoad()
            self.navigationController?.navigationBar.isHidden = true
            self.view.backgroundColor = .white
            
            view.addSubview(readView)
            let rect = DZM_READ_VIEW_RECT
            readView.frame = CGRect(x: rect.minX, y: rect.minY, width: rect.width, height: readView.pageModel.contentSize.height)
            
//            readView.creatMagnifierView(windowPoint: CGPoint(x: 100, y: 100))
        }
        
        // MARK: 页面触摸拖拽处理
        
        /// 触摸开始
        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            
            drag(touches: touches, status: .begin)
        }
        
        /// 触摸移动
        override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
            
            drag(touches: touches, status: .changed)
        }
        
        /// 触摸结束
        override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
            
            drag(touches: touches, status: .end)
        }
        
        /// 触摸取消
        override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
            
            drag(touches: touches, status: .end)
        }
        
        /// 解析触摸事件
        private func drag(touches: Set<UITouch>, status: DZMPanGesStatus) {
            
            if readView.isOpenDrag {
                
                let windowPoint = ((touches as NSSet).anyObject() as? UITouch)?.location(in: view)
          
                if windowPoint != nil {
                    
                    let point = view.convert(windowPoint!, to: readView)
            
                    readView.drag(status: status, point: point, windowPoint: windowPoint!)
                }
            }
        }
        
        lazy var readView: DZMReadLongPressView = {
            let read = DZMReadLongPressView.init(frame: DZM_READ_VIEW_RECT)
            
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.lineSpacing = 5
            
            let attrContent = NSMutableAttributedString(string: TestNoteContent, attributes: [
                .font : UIFont.systemFont(ofSize: 22),
                .paragraphStyle : paragraphStyle
                ])
            
            let model = DZMReadPageModel()
        
            model.content = attrContent
            let maxW = DZM_READ_VIEW_RECT.width
            
            // + 10 画虚线需要
            
            model.contentSize = CGSize(width: maxW, height: DZMCoreText.GetAttrStringHeight(attrString: attrContent , maxW: maxW) + 10)
            model.range = NSRange(location: 0, length: TestNoteContent.length)
            
            read.pageModel = model
            read.noteLinesModel = NoteLinesModel()
            
            return read
        }()


}
